
import datetime
from flask import Blueprint, request
from conn_mysql import ConnectMysql



def access_token_verify_func(access_token, user_id):

    if not all([access_token, user_id]):#access_token值没有给到
        return {
            'status': -1,
            'errorCode': 11,
            'errorMsg': 'access_token为空',
            'resultBody': None,
        }

    with ConnectMysql() as c:

        sql = 'SELECT * FROM access_token WHERE access_token=%s and user_id=%s'
        result = c.select_one(sql, (access_token, user_id))
    if not result:

        return {
            'status': -1,
            'errorCode': 12,
            'errorMsg': 'access_token在数据库中没有找到配对',
            'resultBody': None,
        }

    token_time = result.get('regedate')

    if token_time + datetime.timedelta(hours=2) <= datetime.datetime.now():

        return {

            'status': -1,
            'errorCode': 13,
            'errorMsg': 'access_token已经过期请重新登录',
            'resultBody': None,

        }

    return {
        'status': 1,
        'errorCode': None,
        'errorMsg': None,
        'resultBody': '验证通过',
    }



app_access_token_verify = Blueprint('app_access_token_verify', 'api')

@app_access_token_verify.route('/access_token_verify')
def access_token_verify():

    access_token = request.args.get('access_token')
    user_id = request.args.get('user_id')
    return access_token_verify_func(access_token, user_id)
    
    


    



